package com.ruoyi.safe.service.impl;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.safe.domain.BizSafeFine;
import com.ruoyi.safe.mapper.BizSafeFineMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.safe.mapper.BizSafeBillnoMapper;
import com.ruoyi.safe.domain.BizSafeBillno;
import com.ruoyi.safe.service.IBizSafeBillnoService;

/**
 * 安全罚款台账编号定义Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-11-15
 */
@Service
public class BizSafeBillnoServiceImpl implements IBizSafeBillnoService 
{
    @Autowired
    private BizSafeBillnoMapper bizSafeBillnoMapper;
    @Autowired
    private BizSafeFineMapper bizSafeFineMapper;

    /**
     * 查询安全罚款台账编号定义
     * 
     * @param id 安全罚款台账编号定义主键
     * @return 安全罚款台账编号定义
     */
    @Override
    public BizSafeBillno selectBizSafeBillnoById(Long id)
    {
        return bizSafeBillnoMapper.selectBizSafeBillnoById(id);
    }

    /**
     * 查询安全罚款台账编号定义列表
     * 
     * @param bizSafeBillno 安全罚款台账编号定义
     * @return 安全罚款台账编号定义
     */
    @Override
    public List<BizSafeBillno> selectBizSafeBillnoList(BizSafeBillno bizSafeBillno)
    {
        return bizSafeBillnoMapper.selectBizSafeBillnoList(bizSafeBillno);
    }

    /**
     * 新增安全罚款台账编号定义
     * 
     * @param bizSafeBillno 安全罚款台账编号定义
     * @return 结果
     */
    @Override
    public int insertBizSafeBillno(BizSafeBillno bizSafeBillno)
    {
        bizSafeBillno.setCreateTime(DateUtils.getNowDate());
        bizSafeBillno.setCreateBy(SecurityUtils.getUsername());
        return bizSafeBillnoMapper.insertBizSafeBillno(bizSafeBillno);
    }

    /**
     * 修改安全罚款台账编号定义
     * 
     * @param bizSafeBillno 安全罚款台账编号定义
     * @return 结果
     */
    @Override
    public int updateBizSafeBillno(BizSafeBillno bizSafeBillno)
    {
        bizSafeBillno.setUpdateTime(DateUtils.getNowDate());
        bizSafeBillno.setUpdateBy(SecurityUtils.getUsername());
        return bizSafeBillnoMapper.updateBizSafeBillno(bizSafeBillno);
    }

    /**
     * 批量删除安全罚款台账编号定义
     * 
     * @param ids 需要删除的安全罚款台账编号定义主键
     * @return 结果
     */
    @Override
    public int deleteBizSafeBillnoByIds(Long[] ids)
    {
        return bizSafeBillnoMapper.deleteBizSafeBillnoByIds(ids);
    }

    /**
     * 删除安全罚款台账编号定义信息
     * 
     * @param id 安全罚款台账编号定义主键
     * @return 结果
     */
    @Override
    public int deleteBizSafeBillnoById(Long id)
    {
        return bizSafeBillnoMapper.deleteBizSafeBillnoById(id);
    }

    @Override
    public String getSafeNumber(Long id) {
        BizSafeBillno bizSafeBillno = bizSafeBillnoMapper.selectBizSafeBillnoById(id);
        if(bizSafeBillno == null){
            throw  new SecurityException("该编号定义不存在");
        }
        //拼接
        String billno = bizSafeBillno.getDefHeader() + DateUtil.format(new Date(),"yyyyMM");//F202311
        String no = "";
        //判断如果是第一次新增的话编号为1
        List<BizSafeFine> bizSafeFineList = bizSafeFineMapper.selectList()
                .stream().filter(o -> o.getSafeNumber().contains(bizSafeBillno.getDefHeader())).collect(Collectors.toList());
        List<String> safeNumber = bizSafeFineList.stream().map(BizSafeFine::getSafeNumber)
                .filter(a -> a.contains(billno)).collect(Collectors.toList());
        if(safeNumber.size() == 0){
            no = "1";
            no = StrUtil.fillBefore(no,'0',bizSafeBillno.getSerialLength().intValue());
        }else {
            BizSafeFine bizSafeFine = bizSafeFineList.get(bizSafeFineList.size() - 1);
            Integer a = Integer.parseInt(bizSafeFine.getSafeNumber().substring(7,12))+1;
            bizSafeFine.setSafeNumber(String.format("%05d",a));//F20231100001
            no = String.valueOf(bizSafeFine.getSafeNumber());
            no = StrUtil.fillBefore(no,'0', bizSafeBillno.getSerialLength().intValue());
        }
        return billno + no;
    }
}
